#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "MSTK.h"


#ifdef __cplusplus
extern "C" {
#endif

int MESH_Surf_BuildBoundary(Mesh_ptr mesh) {
  int idx;
  int nv, ne, nf;
  MVertex_ptr mv;
  MEdge_ptr me;
  MFace_ptr mf, mefaces, mvfaces;
  

  /* basic mesh information */
  nv = MESH_Num_Vertices(mesh);
  ne = MESH_Num_Edges(mesh);
  nf = MESH_Num_Faces(mesh);

  /* Mark all the face to be interior (GEnt_MEntDim = 2) */
  idx = 0;
  while((mf = MESH_Next_Face(mesh,&idx)))  
    MF_Set_GEntDim(mf,2);

  /* Mark all the corner vertex */
  idx = 0;
  while((mv = MESH_Next_Vertex(mesh,&idx))) {
    /* Mark all the vertex to be interior first */
    MV_Set_GEntDim(mv,2);
    /* Mark corner vertex */
    mvfaces = MV_Faces(mv);
    if(List_Num_Entries(mvfaces)<2)
      MV_Set_GEntDim(mv,0);
  }
 /* Mark all the boundary edges(GEnt_MEntDim = 1) and boundary vertex*/
  idx = 0;
  while((me = MESH_Next_Edge(mesh,&idx))) {
    /* mark each edge to be interior first */
    ME_Set_GEntDim(me,2);
    mefaces = ME_Faces(me);
    /* mark boundary edge and its endpoints */
    if(List_Num_Entries(mefaces) < 2) {
      ME_Set_GEntDim(me,1);
      /* if not corner vertex */
      if(MV_GEntDim(ME_Vertex(me,0))!=0)
	MV_Set_GEntDim(ME_Vertex(me,0),1);
      if(MV_GEntDim(ME_Vertex(me,1))!=0)
	MV_Set_GEntDim(ME_Vertex(me,1),1);
    }      
  }
  return 1;
}
  
#ifdef __cplusplus
}
#endif

